package com.cs.beneficios.validator;

import java.sql.*;
import com.cs.beneficios.common.ConnectionFactory;
import java.sql.ResultSet;
import java.sql.SQLException;



public class Validaciones 
{

 private ConnectionFactory cf;
 //private database db;
  private Connection conn = null; 
  
  public Validaciones(ConnectionFactory cf)
  { 
     this.cf = cf;
     try{
      this.conn = this.cf.getConnection();
    }catch(SQLException e){}
 //  db = new database();
  
  }//Validaciones
  
  public boolean ValidaUsu (String strusuario)
  {
    
    String strSql = "select nombre from paoyer.tcs_usuario where codigo = ? ";
    String strPar[] = {strusuario};
 
  
    try{
      ResultSet rs = cf.getRecordsRset(strSql,strPar,conn);
       
       if(rs.next()){
            return true;
        }
        else{
            return false;
        }
      
    }catch(SQLException sqle)
    {
       return false;
    }
        
    }
    
    
     public boolean validaActProg(){
   
     String strSql = "select nombre, codigo from tcs_ben_programa where vigencia = 'I' OR SYSDATE > HASTA_VIGENCIA";
     String strPar[] = {};
      
  
    try{
      
      ResultSet rs = cf.getRecordsRset(strSql,null,conn);
       
       if(rs.next()){
            return true;
        }
        else{
            return false;
        }
    }catch(SQLException sqle)
    {
       return false;
     }
   }
   
     public boolean validaActSub(){
   
     String strSql = "select nombre, codigo from tcs_ben_subprograma where vigencia = 'I' ";
     String strPar[] = {};
      
  
    try{

      ResultSet rs = cf.getRecordsRset(strSql,null,conn);
       
       if(rs.next()){
            return true;
        }
        else{
            return false;
        }
      
    }catch(SQLException sqle)
    {
       return false;
     }
   }
   
    public boolean validasubprg(String strsubprg)
    {
       String strSql = "select nombre from tcs_ben_subprograma where UPPER(nombre) like upper ('"+strsubprg.trim()+"') ";           
       String strsub = strsubprg;
       String strPar[] = {strsub};
       
       //System.out.println(strSql);
      try{
         ResultSet rs = cf.getRecordsRset(strSql,null,conn);
       
           if(rs.next()){
                return true;
            }
            else{
                return false;
            }
      
      }catch(SQLException sqle)
       {
       return false;
       }
    
    }
   public boolean validaprog(String strprg)
    {
       String strSql = "select nombre from tcs_ben_programa where UPPER(nombre) like upper('"+strprg.trim()+"') ";
       //String strsub = "%"+strprg+"%";
       //String strPar[] = {strsub};
      try{
         ResultSet rs = cf.getRecordsRset(strSql,null,conn);
       
       if(rs.next()){
            return true;
        }
        else{
            return false;
        }
      
      }catch(SQLException sqle)
       {
       return false;
       }
    
    }
    
    public boolean validasede(String strsede)
    {
       String strSql = "select nombre from tcs_ben_sede where UPPER(nombre) = upper('"+strsede+"') ";
       //String strsub = "%"+strprg+"%";
       //String strPar[] = {strsub};
      try{
         ResultSet rs = cf.getRecordsRset(strSql,null,conn);
       
       if(rs.next()){
            return true;
        }
        else{
            return false;
        }
      
      }catch(SQLException sqle)
       {
       return false;
       }
    
    }
  
   public boolean validaUsuadmin(String strusu)
    {
       String strSql = "select nombre from paoyer.tcs_usuario where nombre like upper (?) and Fcs_Val_Vigencia(VIGENCIA,DESDE_VIGENCIA,HASTA_VIGENCIA) = 'S' ";
       String strPar[] = {strusu};
       
      try{
         ResultSet rs = cf.getRecordsRset(strSql,strPar,conn);
       
       if(rs.next()){
            return true;
        }
        else{
            return false;
        }
      
      }catch(SQLException sqle)
       {
       return false;
       }
    }  
 public boolean ValidaDias(String fecha1, String fecha2, String strdias)
  {
      String strfecha1[] = fecha1.split("-");
      String strfecha2[] = fecha2.split("-");
      
      int anos = (Integer.valueOf(strfecha1[0]).intValue()- Integer.valueOf(strfecha2[0]).intValue())*365;
      int meses =(Integer.valueOf(strfecha1[1]).intValue()-Integer.valueOf(strfecha2[1]).intValue())*365;
      int dias = Integer.valueOf(strfecha1[2]).intValue()- Integer.valueOf(strfecha2[2]).intValue();
     
      int total =  (anos +  meses +  dias)*(-1);
      int diasreut = Integer.parseInt(strdias.trim());
         
      if(diasreut <= total){
          return true;
      }else{
          return false;
      }
  }    
 
 public Connection getConn()
{
    return conn;
}      

  }
  
  
  
  
